﻿#include "FtpServer.hpp"

FtpServer::FtpServer(boost::asio::io_service& ioservice,tcp::endpoint& endpoint):io_service(ioservice),_acceptor(ioservice,endpoint)
{
   
}

FtpServer::~FtpServer()
{

}

void FtpServer::start(){
	 	
	 session_ptr new_session(new ClientSession(io_service));
   _acceptor.async_accept(new_session->getSocket(),
            boost::bind(&FtpServer::handle_accept,this,boost::asio::placeholders::error,new_session));
}

void FtpServer::handle_accept(const boost::system::error_code& error,session_ptr& session)
{
    if(!error)
    {
        //std::cout << "get a new client!" << std::endl;
        //实现对每个客户端的数据处理

        session->start();
        //在这就应该看出为什么要封session类了吧，每一个session就是一个客户端

        session_ptr new_session(new ClientSession(io_service));
        _acceptor.async_accept(new_session->getSocket(),
            boost::bind(&FtpServer::handle_accept,this,boost::asio::placeholders::error,new_session));
    }
}

void FtpServer::log(string& message)
{
	 Log::Logger::getInstance().log(message, Log::Debug);
}